package com.jzy.gulimall.ware.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.jzy.common.exception.BizCodeEnum;
import com.jzy.common.exception.NoStockException;
import com.jzy.common.to.SkuHasStockRespVo;
import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.R;
import com.jzy.gulimall.ware.entity.WareSkuEntity;
import com.jzy.gulimall.ware.service.WareSkuService;
import com.jzy.gulimall.ware.vo.WareSkuLockVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品库存
 *
 * @author jzy
 * @email 1802511725@qq.com
 * @date 2020-07-12 08:58:54
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 提交订单  生成订单
     * 远程调用  锁库存
     */
    @PostMapping("/lockStock")
    public R lockStock(@RequestBody WareSkuLockVo wareSkuLockVo) {
        try {
            wareSkuService.lockStock(wareSkuLockVo);
            return R.ok();
        } catch (NoStockException e) {
            e.printStackTrace();
            return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(), BizCodeEnum.NO_STOCK_EXCEPTION.getMessage());
        }
    }

    /**
     * 远程调用 spu管理 上架
     * 查询sku是否有库存
     * 远程调用 订单确认页
     */
    @PostMapping("/getSkuHasStock")
    @SuppressWarnings("unchecked")
    public R getSkuHasStock(@RequestBody List<Long> skuIds) {
        //sku_id  stock
        List<SkuHasStockRespVo> respVos = wareSkuService.getSkuHasStock(skuIds);
        if (CollectionUtil.isNotEmpty(respVos)) {
            return R.ok().setData(respVos);
        }
        return R.error("没有此商品库存信息");
    }


    /**
     * 库存系统 商品库存 列表
     * page=1&limit=10&skuId=&wareId=
     */
    @RequestMapping("/list")
    @RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPageByCondition(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
